package com.example.FileSearch;

import android.content.Context;
import android.content.ContextWrapper;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by fany on 14-1-22.
 */
public class SQLiteFileRecorder implements IFileRecorder {
    public SQLiteFileRecorder(Context context){
        _helper = new SQLiteFileRecordAccessor(new DatabaseContext(context), "IFileRecorder.db", null, 1);
    }

    @Override
    public String[] SearchFile(String filter) {
        List<String> rets = _helper.Search(filter);
        String[] results = new String[rets.size()];
        rets.toArray(results);
        return results;
    }

    @Override
    public boolean HasLoaded() {
        return _helper.HasLoaded();
    }

    @Override
    public void Clear() {
        _helper.Clear();
    }

    @Override
    public void Record(String filename) {
        _helper.Insert(filename);
    }

    @Override
    public void UnRecord(String filename) {
        _helper.Delete(filename);
    }

    private SQLiteFileRecordAccessor _helper;



    class DatabaseContext extends ContextWrapper {
        public DatabaseContext(Context base){
            super(base);
        }

        @Override
        public File getDatabasePath(String name) {
            boolean sdExist = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
            if(sdExist){
                String dbDir=Environment.getExternalStorageDirectory().getAbsolutePath();
                dbDir += File.separator +"Training";
                File dirFile = new File(dbDir);
                if(!dirFile.exists()){
                    dirFile.mkdirs();
                }
                String dbPath = dbDir+File.separator+name;
                File dbFile = new File(dbPath);
                if(!dbFile.exists()){
                    try {
                        dbFile.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                        dbFile = null;
                    }
                }
                return dbFile;
            }
            return null;
        }

        @Override
        public SQLiteDatabase openOrCreateDatabase(String name, int mode,
                                                   SQLiteDatabase.CursorFactory factory) {
            SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), null);
            return result;
        }

        @Override
        public SQLiteDatabase openOrCreateDatabase(String name, int mode,
                                                   SQLiteDatabase.CursorFactory factory,
                                                   DatabaseErrorHandler errorHandler) {
            SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), null);
            return result;
        }
    }
}
